package com.smec.remes.module.register.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutContractStationBinding;
import com.smec.remes.base.SmecBaseFragment;
import com.smec.remes.bean.SearchContract;
import com.smec.remes.module.register.SmecRegisterPresenter;
import com.smec.remes.module.register.adapter.SmecContractAdapter;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.WmsConstant;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/18.
 */

public class SmecContractStationFragment extends SmecBaseFragment<SmecRegisterPresenter> {

    public static final String CONTRACT="CONTRACT";
    public static final String CONTRACTFAIL="CONTRACTFAIL";
    public static final String contractCheck="contractCheck";
    public static final String contractCheckFail="contractCheckFail";
    private SmecContractAdapter smecContractAdapter;
    private Handler mHandler=new Handler();
    private String  status;
    private int pageNumber=1;
    private String condition="";
    private Boolean first=true;
    private LayoutContractStationBinding layoutContractStationBinding;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        layoutContractStationBinding=DataBindingUtil.inflate(inflater, R.layout.layout_contract_station,container,false);
        smecContractAdapter=new SmecContractAdapter(getActivity());
        layoutContractStationBinding.contractRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        layoutContractStationBinding.contractRecyclerView.setAdapter(smecContractAdapter);
        _firstGet();
        _search();
        return layoutContractStationBinding.getRoot();
    }

    public void _search(){
        layoutContractStationBinding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                layoutContractStationBinding.llPrompt.setVisibility(View.GONE);
//                first=false;
                searchByKeyWord();
            }
        });
        layoutContractStationBinding.edSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
//                    layoutContractStationBinding.llPrompt.setVisibility(View.GONE);
//                    first=false;
                    searchByKeyWord();
                }
                return false;
            }
        });
        layoutContractStationBinding.edSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                layoutContractStationBinding.ivDelete.setVisibility(View.VISIBLE);
                layoutContractStationBinding.ivSearch.setImageResource(R.drawable.smec_remes_lightsearch);
            }
        });
        layoutContractStationBinding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                condition="";
                layoutContractStationBinding.edSearch.setText("");
            }
        });
    }

    private void searchByKeyWord(){
        layoutContractStationBinding.ptr.setEnableRefresh(true);
        CommonUtils.hideImmManager(layoutContractStationBinding.getRoot());
        condition=layoutContractStationBinding.edSearch.getText().toString();
        layoutContractStationBinding.ptr.startRefresh();
    }

    @Override
    public void onResume() {
        super.onResume();
//        first=true;
//        layoutContractStationBinding.llPrompt.setVisibility(View.VISIBLE);
    }

    public void _firstGet(){
        layoutContractStationBinding.ptr.setHeaderView(WmsConstant.getSinaRefreshView());
        layoutContractStationBinding.ptr.setBottomView(new LoadingView(getActivity()));
        layoutContractStationBinding.ptr.setAutoLoadMore(true);
        layoutContractStationBinding.ptr.setEnableOverScroll(false);
        layoutContractStationBinding.ptr.setEnableLoadmore(true);
        layoutContractStationBinding.ptr.setEnableRefresh(true);
        layoutContractStationBinding.ptr.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                pageNumber=1;
                status="onRefresh";
                mPresenter.searchContract(condition,"STATION",pageNumber);
                pageNumber+=1;
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                status="onLoadMore";
                mPresenter.searchContract(condition,"STATION",pageNumber);
                pageNumber+=1;
            }

        });
        layoutContractStationBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                layoutContractStationBinding.ptr.startRefresh();
            }
        },500);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(CONTRACT)
            }
    )

    public void _getContract( ArrayList<SearchContract> arrayList){
        if(CommonUtils.notEmpty(arrayList)){
            smecContractAdapter.getList(arrayList,status);
        }else {
            if(status.equals("onRefresh")){
                smecContractAdapter.clearList();
            }
        }

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(status.equals("onRefresh")){
                    layoutContractStationBinding.ptr.finishRefreshing();
                }else {
                    layoutContractStationBinding.ptr.finishLoadmore();
                }

            }
        },500);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(CONTRACTFAIL)
            }
    )

    public void _contractFail(String error){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(status.equals("onRefresh")){
                    layoutContractStationBinding.ptr.finishRefreshing();
                }else {
                    layoutContractStationBinding.ptr.finishLoadmore();
                }
            }
        },500);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(contractCheck)
            }
    )

    public void _contractCheck(String code){
        layoutContractStationBinding.ptr.startRefresh();
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(contractCheckFail)
            }
    )

    public void _contractCheckFail(String code){
        Toast.makeText(getActivity(),"校验失败",Toast.LENGTH_SHORT).show();
    }




    @Override
    protected SmecRegisterPresenter getPersenter() {
        return new SmecRegisterPresenter(getActivity());
    }

    public static SmecContractStationFragment newInstance(){
        Bundle args = new Bundle();
        SmecContractStationFragment smecContractStationFragment=new SmecContractStationFragment();
        smecContractStationFragment.setArguments(args);
        return smecContractStationFragment;
    }
}
